﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Com.CMS263.PO;
using Com.CMS263.DAO;

namespace Com.CMS263.BLL
{
    public class ProdAttributeBLL:BaseBLL<ProdAttribute>
    {
        ProdAttributeDAO dao = new ProdAttributeDAO();
        ProdGoodsAttrDAO pgdao = new ProdGoodsAttrDAO();
        public ProdAttribute entity = new ProdAttribute();
        public override ProdAttribute GetModel()
        {
            return entity;
        }

        public IList<ProdAttribute> FindAll()
        {
            return dao.FindAll();
        }
        /// <summary>
        /// 根据类型id查找扩展属性
        /// </summary>
        /// <param name="prodtypeid"></param>
        /// <returns></returns>
        public IList<ProdAttribute> GetAllByType(int prodtypeid)
        {
            return dao.GetAllByType(prodtypeid);
        }

        #region 给据id查询实体
        /// <summary>
        /// 给据id查询实体
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ProdAttribute GetPrById(int id)
        {
            return dao.GetPrById(id);
        }
        #endregion
        
        public string ShowAttr(ProdAttribute EA)
        {
            string html = "";
            string note = "";
            if (EA.IsNull)
            {
                note = "(此为必填项)";
            }
            if (EA.AttrType == 1)
            {
                html = "<input type='text' name='" + EA.AttrEnName + "' id='" + EA.AttrEnName + "' value='" + (string.IsNullOrEmpty(EA.DefaultValue) ? "" : EA.DefaultValue) + "' />";
            }
            else if (EA.AttrType == 2)
            {
                html = " <textarea  name='" + EA.AttrEnName + "' id='" + EA.AttrEnName + "' cols='45' rows='5'>" + (string.IsNullOrEmpty(EA.DefaultValue) ? "" : EA.DefaultValue) + "</textarea>";
            }
            
            else if (EA.AttrType == 3)
            {
                html += "<span style='color:red'>如果是从word文档粘贴的内容,请选择清除或过滤样式</span><input type='hidden' id='" + EA.AttrEnName + "' name='" + EA.AttrEnName + "' value='' /><input type='hidden' id='" + EA.AttrEnName + "___Config' value='HtmlEncodeOutput=false' /><iframe id='" + EA.AttrEnName + "___Frame' src='/fckeditor/editor/fckeditor.html?InstanceName=" + EA.AttrEnName + "&amp;Toolbar=Default' width='100%' height='250px' frameborder='no' scrolling='no'></iframe>";
            }
            else if (EA.AttrType == 4 )
            {
                html += " <input type='file' name='" + EA.AttrEnName + "' id='" + EA.AttrEnName + "' />";
            }
            else if (EA.AttrType == 5)
            {
                html += " <input type='file' name='" + EA.AttrEnName + "' id='" + EA.AttrEnName + "' />";
            }
            else if (EA.AttrType == 8)
            {
                html = "<select name='" + EA.AttrEnName + "' id='" + EA.AttrEnName + "'>";
                string[] arr = EA.AttrValue.Split(' ');
                for (int i = 0; i < arr.Length; i++)
                {
                    if (arr[i].Length > 0)
                    {
                        html += " <option value='" + arr[i] + "'>" + arr[i] + "</option>";
                    }
                }
                html += "</select>";
            }
            else if (EA.AttrType == 6)
            {
                string[] arr = EA.AttrValue.Split(' ');
                for (int i = 0; i < arr.Length; i++)
                {
                    if (arr[i].Length > 0)
                    {
                        if (i == 0)
                        {
                            html += "<input type='radio' name='" + EA.AttrEnName + "' checked='checked' value='" + arr[i] + "'>"+arr[i];
                        }
                        else
                        {
                            html += "<input type='radio' name='" + EA.AttrEnName + "' value='" + arr[i] + "'>" + arr[i];
                        }
                    }
                }
                
            }
            else if (EA.AttrType == 7)
            {
                string[] arr = EA.AttrValue.Split(' ');
                for (int i = 0; i < arr.Length; i++)
                {
                    if (arr[i].Length > 0)
                    {                       
                        html += "<input type='checkbox' name='" + EA.AttrEnName + "' value='" + arr[i] + "'>" + arr[i];
                    }
                }
            }
            return html + "&nbsp;&nbsp;&nbsp;&nbsp;" + note;
        }

        public string ShowAttr(ProdAttribute EA, int id)
        {
            string html = "";
            string note = "";
            if (EA.IsNull)
            {
                note = "(此为必填项)";
            }
            if (EA.AttrType == 1)
            {
                html = "<input type='text' name='" + EA.AttrEnName + "' id='" + EA.AttrEnName + "' value='" + (pgdao.GetValueById(id, EA.Id)) + "' />";
            }
            else if (EA.AttrType == 2)
            {
                html = " <textarea  name='" + EA.AttrEnName + "' id='" + EA.AttrEnName + "' cols='45' rows='5'>" + (pgdao.GetValueById(id, EA.Id)) + "</textarea>";
            }
            
            else if (EA.AttrType == 3)
            {
                html += "<span style='color:red'>如果是从word文档粘贴的内容,请选择清除或过滤样式</span><input type='hidden' id='" + EA.AttrEnName + "' name='" + EA.AttrEnName + "' value='" + pgdao.GetValueById(id, EA.Id).Replace("'", "’") + "' /><input type='hidden' id='" + EA.AttrEnName + "___Config' value='HtmlEncodeOutput=false' /><iframe id='" + EA.AttrEnName + "___Frame' src='/fckeditor/editor/fckeditor.html?InstanceName=" + EA.AttrEnName + "&amp;Toolbar=Default' width='100%' height='250px' frameborder='no' scrolling='no'></iframe>";
            }
            else if (EA.AttrType == 4)
            {
                html += " <input type='file' name='" + EA.AttrEnName + "' id='" + EA.AttrEnName + "' />" + "<br/><img src='/ArtUpFile/" + (pgdao.GetValueById(id, EA.Id)) + "' style='width:200px' / >";
            }
            else if (EA.AttrType == 5)
            {
                html += " <input type='file' name='" + EA.AttrEnName + "' id='" + EA.AttrEnName + "' />" + "<a href='/ArtUpFile/" + (pgdao.GetValueById(id, EA.Id)) + "' target='_blank' >查看文件</a>";
            }
            else if (EA.AttrType == 8)
            {
                html = "<select name='" + EA.AttrEnName + "' id='" + EA.AttrEnName + "'>";
                string[] arr = EA.AttrValue.Split(' ');
                for (int i = 0; i < arr.Length; i++)
                {
                    if (arr[i].Length > 0)
                    {
                        if (arr[i] == pgdao.GetValueById(id, EA.Id))
                        {
                            html += " <option selected='selected' value='" + arr[i] + "'>" + arr[i] + "</option>";
                        }
                        else
                        {
                            html += " <option value='" + arr[i] + "'>" + arr[i] + "</option>";
                        }
                    }
                }
                html += "</select>";
            }
            else if (EA.AttrType == 6)
            {
                string[] arr = EA.AttrValue.Split(' ');
                string val = pgdao.GetValueById(id, EA.Id);
                for (int i = 0; i < arr.Length; i++)
                {
                    if (arr[i].Length > 0)
                    {
                        if (arr[i] == val)
                        {
                            html += "<input type='radio' name='" + EA.AttrEnName + "' checked='checked' value='" + arr[i] + "'>" + arr[i];
                        }
                        else
                        {
                            html += "<input type='radio' name='" + EA.AttrEnName + "' value='" + arr[i] + "'>" + arr[i];
                        }
                    }
                }

            }
            else if (EA.AttrType == 7)
            {
                string[] arr = EA.AttrValue.Split(' ');
                string val = pgdao.GetValueById(id, EA.Id);
                for (int i = 0; i < arr.Length; i++)
                {
                    if (arr[i].Length > 0)
                    {
                        if (val.IndexOf(arr[i]) > 0)
                        {
                            html += "<input type='checkbox' checked='checked' name='" + EA.AttrEnName + "' value='" + arr[i] + "'>" + arr[i];
                        }
                        else
                        {
                            html += "<input type='checkbox' name='" + EA.AttrEnName + "' value='" + arr[i] + "'>" + arr[i];
                        }
                    }
                }
            }
            return html + "&nbsp;&nbsp;&nbsp;&nbsp;" + note;
        }

          #region 根据ProdTypeId查询个数
        /// <summary>
        /// 根据ProdTypeId查询个数
        /// </summary>
        /// <param name="ptid"></param>
        /// <returns></returns>
        public int GetCountByProdTypeId(int ptid)
        {
            return dao.GetCountByProdTypeId(ptid);
        }
          #endregion

         #region 根据ProdTypeId删除相关信息
        /// <summary>
        /// 根据ProdTypeId删除相关信息
        /// </summary>
        /// <param name="ptid"></param>
        /// <returns></returns>
        public int DelByProdTypeId(int ptid)
        {
            return dao.DelByProdTypeId(ptid);
        }
       #endregion

        public string GetValueById(ProdAttribute item, int id)
        {
           return pgdao.GetValueById(id, item.Id);
        }
    }
}
